package tripx.siteget.start;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import tripx.update.City;
import tripx.update.Food;
import tripx.update.Site;

import com.google.gson.Gson;

public class Client {

	private static Client client;

	private XmlRpcClient xmlRpcClient;

	private UpdateConfig clientConfig;

	private Client() {
		xmlRpcClient = new XmlRpcClient();
		clientConfig = UpdateConfig.getInstance();
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		try {
			config.setServerURL(new URL(clientConfig.getServerAddr()));
			config.setEnabledForExtensions(true);
			xmlRpcClient.setConfig(config);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public static Client getInstance() {
		if (client == null) {
			client = new Client();
		}
		return client;
	}

	public void addCity(City city) {
		System.out.println("Add " + city.getCityName());
		Gson gson = new Gson();
		String jsonStr = gson.toJson(city);
		try {
			xmlRpcClient.execute("SiteService.addCity",
					new Object[] { jsonStr });
		} catch (XmlRpcException e) {
			System.err.println("Add City Failed.");
		}
	}

	public void addSite(Site site) {
		Gson gson = new Gson();
		String jsonStr = gson.toJson(site);
		try {
			xmlRpcClient.execute("SiteService.addSite",
					new Object[] { jsonStr });
		} catch (XmlRpcException e) {
			System.err.println("Add City Failed.");
		}
	}

	public void addFood(Food food) {
		Gson gson = new Gson();
		String jsonStr = gson.toJson(food);
		try {
			xmlRpcClient.execute("SiteService.addFood",
					new Object[] { jsonStr });
		} catch (XmlRpcException e) {
			System.err.println("Add City Failed.");
		}
	}

	public void uploadSiteImage(String imageUrl, String cityName,
			String siteName) {
		try {
			BufferedImage image = ImageIO.read(new URL(imageUrl));
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ImageIO.write(image, "JPEG", out);
			xmlRpcClient.execute("SiteService.uploadSiteImage", new Object[] {
					out.toByteArray(), cityName, siteName });
		} catch (IOException | XmlRpcException e) {
			System.err.println("Upload Site Image Failed");
		}
	}

	public void uploadFoodImage(String imageUrl, String cityName,
			String foodName) {
		try {
			BufferedImage image = ImageIO.read(new URL(imageUrl));
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ImageIO.write(image, "JPEG", out);
			xmlRpcClient.execute("SiteService.uploadFoodImage", new Object[] {
					out.toByteArray(), cityName, foodName });
		} catch (IOException | XmlRpcException e) {
			System.err.println("Upload Food Image Failed");
		}
	}
}
